<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/fn"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="cfn" uri="http://com.abou/cfn"%>
<%@page import="com.abou.constant.product.*"%>
<%@page import="com.abou.mvc.controller.admin.HomeController"%>
<%@page import="com.abou.mvc.controller.ControllerUtils"%>
<%@ include file="../constants.jsp"%>

<%-- The page URL --%>
<c:url var="pageUrl" value="/admin/home.do" />
<%-- The confirmation messages --%>
<c:set var="confirmCreation"  value="${param.confirmFor == 'productCreation'}"/>
<c:set var="confirmModification"  value="${param.confirmFor == 'productModification'}"/>
<c:set var="confirmDeletion"  value="${param.confirmFor == 'deleted'}"/>

<c:if test="${confirmCreation || confirmDeletion || confirmModification}">
   <c:choose>
      <c:when test="${confirmCreation}">
       <spring:theme var="message" code="admin.home.confirm.product.creation"/>
     </c:when>
      <c:when test="${confirmModification}">
       <spring:theme var="message" code="admin.home.confirm.product.modification"/>
     </c:when>
      <c:when test="${confirmDeletion}">
       <spring:theme var="message" code="admin.home.confirm.product.deletion"/>
     </c:when>
   </c:choose>
   <script type="text/javascript">
     	$(document).ready(function() {
   	 	 	showBodyBox('', '${message}', 'confirm', true, true);
     	});
   </script>
</c:if>

<%-- The search form --%>
<form:form modelAttribute="searchProductsRequest" cssClass="basic"
	name="productsSearchForm" action="${pageUrl}">

	<%-- The global errors message --%>
	<jsp:include page="/WEB-INF/jsp/common/form/errors.jsp">
		<jsp:param name="code" value="form.errors" />
	</jsp:include>

	<%-- The name field--%>
	<jsp:include page="/WEB-INF/jsp/common/form/form-field-text.jsp">
		<jsp:param name="code" value="field.product.name" />
		<jsp:param name="path" value="name" />
		<jsp:param name="focused" value="focused" />
	</jsp:include>

	<%-- The product type --%>
	<c:set var="productTypes" value="<%= EProductType.values() %>"
		scope="request" />
	<jsp:include page="/WEB-INF/jsp/common/form/form-field-select.jsp">
		<jsp:param name="code" value="field.product.type" />
		<jsp:param name="path" value="productType" />
		<jsp:param name="valuesVarName" value="productTypes" />
		<jsp:param name="fixedWidth" value="true" />
		<jsp:param name="addAllValue" value="true" />
	</jsp:include>

	<%-- The submit button --%>
	 <p class="row">
		<label>&nbsp;</label>
		<span class="buttonsList">
			<jsp:include page="/WEB-INF/jsp/common/cmp/button.jsp">
			<jsp:param name="id" value="submitBt"/>
			<jsp:param name="label" value="form.submit.search"/>
			<jsp:param name="type" value="submit"/>
			</jsp:include>
		</span>
	</p>


	<%-- Hidden fields --%>
	<input type="hidden" name="pageIndex" id="pageIndex" value="0" />

</form:form>

<%-- Displaying the result of the search --%>
<c:if test="${not empty searchProductsResult}">

	<%-- The label notifying the nb of lines in the result --%>
	<ul class="search-results">
    <li>
      <c:choose>
        <c:when test="${searchProductsResult.totalRowsNumber == 0}">
          	<spring:theme code="search.result.none" />
        </c:when>
        <c:when test="${searchProductsResult.totalRowsNumber == 1}">
          	<spring:theme code="search.result.one" arguments="${searchProductsResult.totalRowsNumber}"/>
        </c:when>
        <c:otherwise>
          	<spring:theme code="search.result.many" arguments="${searchProductsResult.totalRowsNumber}"/>
        </c:otherwise>
      </c:choose>
    </li>
	</ul>

  	<%-- Displaying the table, if results exist --%>
 	<c:if test="${not empty searchProductsResult.products}">

		<%-- The paging widget container --%>
		<p id="pagingContainer">
		  <jsp:include page="/WEB-INF/jsp/common/cmp/paging.jsp">
		    <jsp:param name="pageIndex" value="${searchProductsRequest.pageIndex}"/>
		    <jsp:param name="totalRowsNb" value="${searchProductsResult.totalRowsNumber}"/>
		    <jsp:param name="nbRowsPerPage" value="${searchProductsRequest.rowsNbByPage}"/>
		    <jsp:param name="formId" value="pageIndexForm"/>
		    <jsp:param name="formParam" value="pageIndex"/>
		  </jsp:include>

		  <%-- The form used to change the page --%>
		  <form:form modelAttribute="searchProductsRequest" id="pageIndexForm" name="pageIndexForm"
		      cssStyle="display:none;"  action="${pageUrl}">
		    <form:hidden path="name"/>
		    <form:hidden path="productType"/>
		    <form:hidden path="description"/>
		    <form:hidden path="rowsNbByPage"/>
		    <form:hidden path="pageIndex"/>
		  </form:form>
		</p>

		<%-- Displaying the table --%>
	    <c:set var="tableId" value="productsTable"/>
	    <c:set var="modifyProductPageBaseUrl" value="/admin/product/modify.do"/>

	    <table cellspacing="0" class="dataTable tree" id="${tableId}">

	        <%-- The caption --%>
	        <caption class="actions">

	          <%-- The table rows number chooser --%>
	          <span id="rowsNbChooser">
	            <form:form modelAttribute="searchProductsRequest" id="tableParamsForm" name="tableParamsForm"
	                 action="${pageUrl}">
	              <label><spring:theme code='search.page.rows.nb' /></label>
	              <form:select path="rowsNbByPage" multiple="false"
	                id="rowsNbByPage" onchange="$('#tableParamsForm').submit();">
	                 <form:options items="<%=HomeController.TABLE_ROWS_NBS_PER_PAGE %>"/>
	              </form:select>
				    <form:hidden path="name"/>
				    <form:hidden path="productType"/>
				    <form:hidden path="description"/>
				    <form:hidden path="rowsNbByPage"/>
				    <form:hidden path="pageIndex"/>
	              	<input type="hidden" id="pageIndex" name="pageIndex" value="0"/>
	              	<input type="hidden" name="rowsNbModif" value="true"/>
	            </form:form>
	          </span>

			<%-- The left buttons list --%>
			<span class="buttonsList">

				<%-- The create button --%>
				<jsp:include page="/WEB-INF/jsp/common/cmp/button.jsp">
				  <jsp:param name="id" value="createBt"/>
				  <jsp:param name="label" value="admin.home.product.create"/>
				  <jsp:param name="type" value="button"/>
				  <jsp:param name="small" value="true"/>
				  <jsp:param name="onclick" value="window.location.href='/abou/admin/product/create.do'"/>
				</jsp:include>

				<%-- The delete button --%>
				<jsp:include page="/WEB-INF/jsp/common/cmp/button.jsp">
				  <jsp:param name="class" value="deleteBt"/>
				  <jsp:param name="label" value="admin.home.product.delete"/>
				  <jsp:param name="type" value="button"/>
				  <jsp:param name="small" value="true"/>
				  <jsp:param name="disabled" value="true"/>
				  <jsp:param name="onclick" value="deleteProducts();"/>
				</jsp:include>
			</span>
	        </caption>

			<%-- The table header --%>
	        <thead>
	          <tr>
	            <%-- The checkboxes header --%>
	            <th class="check">
	             <input type="checkbox" id="mainCheckBox"
	                onclick="handleTableCheckBoxSel('${tableId}');"
	                onmouseout="hideInfoBox();"/>
	            </th>
	             <%-- The product id --%>
	            <jsp:include page="/WEB-INF/jsp/common/table/table-header.jsp">
	              <jsp:param name="code" value="field.product.id"/>
	            </jsp:include>
	            <%-- The product name --%>
	            <jsp:include page="/WEB-INF/jsp/common/table/table-header.jsp">
	              <jsp:param name="code" value="field.product.name"/>
	            </jsp:include>
	            <%-- The product price --%>
	            <jsp:include page="/WEB-INF/jsp/common/table/table-header.jsp">
	              <jsp:param name="code" value="field.product.price"/>
	            </jsp:include>
	            <%-- The product image type --%>
	            <jsp:include page="/WEB-INF/jsp/common/table/table-header.jsp">
	              <jsp:param name="code" value="field.product.image"/>
	            </jsp:include>
	            <%-- The product type --%>
	            <jsp:include page="/WEB-INF/jsp/common/table/table-header.jsp">
	              <jsp:param name="code" value="field.product.type"/>
	            </jsp:include>
	          </tr>
	        </thead>

			<%-- The table body --%>
        	<tbody>
			  <%-- products --%>
	          <c:forEach items="${searchProductsResult.products}" var="product" varStatus="productStatus">
			  <tr class="${productStatus.first ? 'firstRow' : ''}">
			  	<td class="check"><input type="checkbox" id="action_check_${product.id}" name="${product.id}"/></td>
				<%-- Adding the product ID cell --%>
                <td>${cfn:format(pageContext, product.id)}</td>
                <%-- Adding the product name cell --%>
				<c:set var="productName" value="${cfn:format(pageContext, product.name)}"/>
                <td>
                    <c:url var="productUrl" value="${modifyProductPageBaseUrl}">
                      <c:param name="productId" value="${product.id}"/>
                    </c:url>
	                <%-- The link --%>
                    <a class="open" href="${productUrl}">${productName}</a>
                </td>
                <%-- Adding the product price cell --%>
                <td>${cfn:format(pageContext, product.price)}</td>
				<%-- Adding the product image cell --%>
				 <c:set var="imageName" value="${cfn:format(pageContext, product.imagePath1)}"/>
                <td><img src="${imageName}" alt="${productName}" ALIGN=MIDDLE></td>
                <%-- Adding the product type cell --%>
                <td>${cfn:format(pageContext, product.productType)}</td>
			  </tr>
	          </c:forEach>

			  <%-- The empty row if no lines were found --%>
	          <c:if test="${searchProductsResult.totalRowsNumber == 0}">
	            <tr><td colspan="6" class="nospace">&nbsp;</td></tr>
	          </c:if>

        	</tbody>
		</table>

		<%-- The paging widget container --%>
	    <p id="pagingContainer">
	      <jsp:include page="/WEB-INF/jsp/common/cmp/paging.jsp">
	        <jsp:param name="pageIndex" value="${searchProductsRequest.pageIndex}"/>
	        <jsp:param name="totalRowsNb" value="${searchProductsResult.totalRowsNumber}"/>
	        <jsp:param name="nbRowsPerPage" value="${searchProductsRequest.rowsNbByPage}"/>
	        <jsp:param name="formId" value="pageIndexForm"/>
	        <jsp:param name="formParam" value="pageIndex"/>
	      </jsp:include>
	    </p>

<%-- The javascript --%>
<script type="text/javascript" charset="UTF-8" src="${jsFolder}table-checkboxes-routines.js"></script>

<script type="text/javascript">
$(document).ready(function() {

	$("img").each(function(){
		if(this.width > 40 || this.height > 40){
			this.width = 40;
			this.height = 40;
		}
  	});

	<%-- Initializing the javascript features for the header long titles,
	  the rows selection and the checkboxes actions --%>
	  initTableRowSelWithHref('${tableId}');

	  // building the actions data array
	  var actionsData = [['deleteBt', -1, '']];
	  initTableCheckboxesActions('${tableId}', actionsData);
	});

	/**
	 * Launches the action to delete products.
	 * @return void
	 */
	function deleteProducts(){

	  // preparing the confirmation dialog
	  var msgLbl = getSelectedRowsNb('${tableId}') > 1 ?
	          '<spring:theme code="admin.home.product.delete.confirm.msg.many"/>'
	          : '<spring:theme code="admin.home.product.delete.confirm.msg.one"/>';

	  var actionFn = function(){
	      execActionForTableRows('${tableId}',
	        '<c:url value="/admin/product/delete.do"/>', 'productIds');
	  };

	  // showing the confirmation dialog
	  showDialogBox('<spring:theme code="admin.home.product.delete.confirm.title"/>',
	  	msgLbl, 'confirm', '<spring:theme code="admin.home.product.delete.confirm.yes"/>',
	      '<spring:theme code="admin.home.product.delete.confirm.no"/>', actionFn)
	}
</script>
	</c:if>
</c:if>